package fw;

import java.io.File;
import java.net.Inet4Address;
import java.net.UnknownHostException;

import fw.utils.PropertyLoader;

public class Config {
	private final PropertyLoader appProperties;
	private final PropertyLoader testProperties;
	private String serverIP;
	private static String JENKINS_IP = "someIP";

	/*** APPLICATION PROPERTIES ***/
	/* WebDriverWait */
	public static Integer WEBDRIVER_WAIT;
	// GoogleChrome driver
	public static File DRIVER_CHROME_PATH;
	// InternetExplorer driver
	public static File DRIVER_IE_PATH;
	/* Browsers */
	public static String BROWSER_FF;
	public static String BROWSER_CH;
	public static String BROWSER_IE;
	/* Projects */
	public static String PROJECT_TELERIK;
	public static String PROJECT_MYDVDCOLLECTION;
	/* Proxies */
	public static String PROXY_1;
	public static String PROXY_2;
	/* Locations */
	public static String LOCATION_1;
	public static String LOCATION_2;
	/* Data Base */
	public static String DB_NAME;
	public static String DB_USER;
	public static String DB_PASSWORD;

	/*** TEST PROPERTIES ***/
	public static String TEST_TYPE;
	public static String TEST_FUNCTIONALITY;
	public static String PROJECT;
	public static String BROWSER_NAME;
	public static String PROXY;

	public String JOB_NAME = "";

	public Config(ApplicationManager p_app) {
		try {
			serverIP = Inet4Address.getLocalHost().getHostAddress();
		} catch (UnknownHostException ex) {
			ex.printStackTrace();
		}

		if (!serverIP.equals(JENKINS_IP)) {
			appProperties = new PropertyLoader(System.getProperty("user.dir")
					+ "/src/fw/config/application_local.properties");
			testProperties = new PropertyLoader(System.getProperty("user.dir") + "/src/fw/config/test_local.properties");
		} else {
			appProperties = new PropertyLoader(System.getProperty("user.dir")
					+ "/target/classes/application.properties");
			testProperties = new PropertyLoader(System.getProperty("user.dir") + "/target/classes/test.properties");
		}

		/*** TEST PROPERTIES ***/
		TEST_TYPE = getTestProperty("test.type");
		TEST_FUNCTIONALITY = getTestProperty("test.functionality");
		BROWSER_NAME = getTestProperty("browser.name");
		PROXY = getTestProperty("proxy");

		/*** APPLICATION PROPERTIES ***/
		/* WebDrivers */
		WEBDRIVER_WAIT = Integer.parseInt(getAppProperty("webdriver.wait"));
		// GoogleChrome driver
		DRIVER_CHROME_PATH = new File(System.getProperty("user.dir") + "/src/common/res/chromedriver.exe");
		// InternetExplorer driver
		DRIVER_IE_PATH = new File(System.getProperty("user.dir") + "/src/common/res/IEDriverServer.exe");
		/* Browsers */
		BROWSER_FF = "firefox";
		BROWSER_CH = "chrome";
		BROWSER_IE = "ie";
		/* Projects */
		PROJECT_TELERIK = getAppProperty("project.telerik.baseUrl");
		PROJECT_MYDVDCOLLECTION = getAppProperty("project.mydvdcollection.baseUrl");
		/* Location */
		LOCATION_1 = getAppProperty("location.1");
		LOCATION_2 = getAppProperty("location.2");
		/* Proxies */
		PROXY_1 = getAppProperty("proxy.1");
		PROXY_2 = getAppProperty("proxy.2");
		/* Data Base */
		DB_NAME = getAppProperty("db.name");
		DB_USER = getAppProperty("db.user");
		DB_PASSWORD = getAppProperty("db.password");
	}

	/**
	 * Get application property
	 * 
	 * @param propertyName
	 * @return
	 */
	private String getAppProperty(String propertyName) {
		String propertyValue = appProperties.load(propertyName);

		return propertyValue;
	}

	/**
	 * Get test property
	 * 
	 * @param propertyName
	 * @return
	 */
	private String getTestProperty(String propertyName) {
		String propertyValue = testProperties.load(propertyName);

		return propertyValue;
	}
}
